package com.second.system.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.second.common.pojo.User;
import com.second.common.vo.Result;
import com.second.system.service.UserService;
import com.second.system.vo.LoginVo;
import com.second.system.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * @program: second-education-parent
 * @description:
 * @author: 王子朋
 * @create: 2023-05-22 19:16
 **/
@RestController
@RequestMapping("/system/user")
@Api(tags = "登录接口类")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @PostMapping("/login")
    @ApiOperation(value = "登录接口")
    public Result login(@RequestBody LoginVo loginVo) {
        User user = userService.findByNameAndPwd(loginVo.getUsername(), loginVo.getPassword());
        if (user != null) {
            if (user.getIsDeleted().equals(0)) {
                //表示成功
                String token = IdUtil.simpleUUID();
                user.setPassword(null);
                redisTemplate.opsForValue().set("user::" + token, JSON.toJSONString(user), 30, TimeUnit.MINUTES);
                return Result.success(token);
            } else {
                //表示该账号已经被冻结
                return Result.fail("该账号被冻结了，请联系管理员");
            }
        }
        return Result.fail("账号或密码错误");
    }
    @GetMapping("info")
    @ApiOperation(value = "获取当前用户信息")
    public Result info(HttpServletRequest request){
        //1.从请求头获取token信息
        String token = request.getHeader("token");
        //2.从redis中获取当前登录者的信息
        String json = redisTemplate.opsForValue().get("user::" + token);
        User user = JSON.parseObject(json, User.class);
        return Result.success(user);
    }

    @GetMapping("logout")
    @ApiOperation(value = "退出登录")
    public Result logout(HttpServletRequest request){
        //1.从请求头获取token信息
        String token = request.getHeader("token");
        Boolean delete = redisTemplate.delete("user::" + token);
        return Result.success();
    }
    @ApiOperation("分页条件查询")
    @PostMapping("/list/{current}/{pageSize}")
    public Result list(@PathVariable Integer current, @PathVariable Integer pageSize, @RequestBody UserVo uservo){
        Page<User> userPage = userService.findByConditionPage(current, pageSize, uservo);
        return Result.success(userPage);
    }
    @ApiOperation("修改用户状态")
    @GetMapping("/status")
    public Result status(@RequestParam String id, @RequestParam Integer status){
        int row = userService.updateStatus(id, status);
        return Result.success();
    }

}
